//
//  JZPageController.m
//  eStudy(comprehensive)
//
//  Created by 飞猪 on 2018/10/26.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JZPageController.h"

@interface JZPageController ()

@property (nonatomic, strong) JZPageContentView *pageContentView;

#pragma mark - 记录
@property (nonatomic, strong) NSArray<NSString *> *titles;
@property (nonatomic) NSArray<UIViewController *> *viewControllers;
@property (nonatomic, strong) JZPageControllerConfig *config;

@end

@implementation JZPageController

#pragma mark - LifeCycle
- (instancetype)initWithFrame:(CGRect)frame {
    return [self initWithFrame:frame config:[JZPageControllerConfig new] titles:@[] viewControllers:@[]];
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self createViews];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
}

#pragma mark - APIs
- (instancetype)initWithFrame:(CGRect)frame
                       config:(JZPageControllerConfig *)config
                       titles:(NSArray<NSString *> *)titles
              viewControllers:(NSArray<UIViewController *> *)controllers {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.titles = titles;
        self.viewControllers = controllers;
        self.config = config;
        
        [self createViews];
    }
    return self;
}

#pragma mark - CreateViews
- (void)createViews {
    self.backgroundColor = [UIColor blueColor];
    self.clipsToBounds = YES;
    
    [self addSubview:self.pageCategoryView];
    
    self.pageCategoryView.frame = CGRectMake(0, 0, self.bounds.size.width, self.config.categoryViewHeight);
    
    [self addSubview:self.pageContentView];
    
    self.pageContentView.frame = CGRectMake(CGRectGetMinX(self.pageCategoryView.frame), CGRectGetMaxY(self.pageCategoryView.frame), self.pageCategoryView.frame.size.width, self.bounds.size.height - self.pageCategoryView.frame.size.height);
    
    self.pageCategoryView.delegate = self.pageContentView;
    self.pageContentView.delegate = self.pageCategoryView;
}

#pragma mark - Lazyload

- (JZPageCategoryView *)pageCategoryView {
    if (!_pageCategoryView) {
        
        JZPageCategoryView *pageCategoryView = [[JZPageCategoryView alloc] initWithFrame:CGRectZero config:self.config titles:self.titles selectedIndex:self.config.selectedIndex];
        
        _pageCategoryView = pageCategoryView;
    }
    return _pageCategoryView;
}

- (JZPageContentView *)pageContentView {
    if (!_pageContentView) {
        
        JZPageContentView *pageContentView = [[JZPageContentView alloc] initWithFrame:CGRectZero config:self.config viewControllers:self.viewControllers];
        
        _pageContentView = pageContentView;
    }
    return _pageContentView;
}


@end
